package xyz.kongbai121.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.kongbai121.demo.domain.Opinion;
import xyz.kongbai121.demo.domain.Tree;
import xyz.kongbai121.demo.mapper.*;
import xyz.kongbai121.demo.service.ITreeService;
import xyz.kongbai121.demo.util.UserPowerException;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TreeServiceImpl implements ITreeService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OpinionMapper opinionMapper;
    @Autowired
    private TreeMapper treeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String save(String head, String body, String imgPath,String token, Integer classify) {
        String openid = redisTemplate.opsForValue().get(token);
        Integer state = userMapper.getState(openid);
        if (state == 1) {
            //TODO 保存树洞
            Tree tree = new Tree();
            tree.setClassify(classify);
            tree.setHead(head);
            tree.setBody(body);
            tree.setImage(imgPath);
            tree.setReleaseTime(new Date());
            tree.setOpenid(openid);
            treeMapper.insert(tree);
            //TODO  用户发布树洞数加一
            userMapper.addTreeHoleNum(openid);
            return "发布成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public List<Tree> list(Integer classify) {
        //TODO 查询所有文章
        List<Tree> list = treeMapper.list(classify);
        for (Tree tree : list) {
            String goodNum = redisTemplate.opsForValue().get("treeGoodNum"+tree.getId());
            String comment = redisTemplate.opsForValue().get("treeOpinionNum"+tree.getId());
            if (goodNum == null||goodNum.equals("null")) goodNum = "0";
            if (comment == null||comment.equals("null")) comment = "0";
            tree.setGoodNum(Integer.valueOf(goodNum));
            tree.setOpinionNum(Integer.valueOf(comment));
        }
        return list;
    }

    @Override
    public Tree get(Long id) {
        //TODO  查询指定文章
        Tree tree = treeMapper.get(id);
        //TODO  点击量加1
        redisTemplate.boundValueOps("treeGoodNum"+id).increment(1);
        return tree;
    }

    @Override
    public String delete(Long id, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Tree tree = treeMapper.selectById(id);
        if (tree.getOpenid()!=null && tree.getOpenid().equals(openid) || userMapper.getAdmin(openid) == 1){
            //如果有图片
            if (tree.getImage() != null && !tree.getImage().isEmpty()){
                //TODO 将要删除本地图片的地址存入Redis
                redisTemplate.opsForList().rightPush("overImages",tree.getImage());
            }
            //TODO 删除文章
            treeMapper.deleteById(id);
            //TODO 删除文章对应的所有评论
            opinionMapper.delete(new UpdateWrapper<Opinion>().eq("tree_id",id));
            //TODO 用户发表文章数减1
            userMapper.subTreeHoleNum(openid);;
            return "删除成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }
}
